package com.oa.process.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.common.result.Result;
import com.oa.model.process.Process;
import com.oa.model.process.ProcessTemplate;
import com.oa.model.process.ProcessType;
import com.oa.process.service.OaProcessRecordService;
import com.oa.process.service.OaProcessService;
import com.oa.process.service.OaProcessTemplateService;
import com.oa.process.service.OaProcessTypeService;
import com.oa.vo.process.ApprovalVo;
import com.oa.vo.process.ProcessFormVo;
import com.oa.vo.process.ProcessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Admin
 * @title: ProcessApiController
 * @projectName Cloud_OA_Office
 * @description: ProcessApiController
 * @date 2024/11/10 10:38
 */
@RestController
@Api(tags = "审批流管理接口")
@RequestMapping(value = "/admin/process")
@CrossOrigin //跨域
public class ProcessApiController {
    @Autowired
    private OaProcessTypeService oaProcessTypeService;
    @Autowired
    private OaProcessTemplateService oaProcessTemplateService;
    @Autowired
    private OaProcessService oaProcessService;
    @Autowired
    private OaProcessRecordService oaProcessRecordService;
    @ApiOperation(value = "获取全部审批分类及模板列表")
    @GetMapping(value="/findProcessType")
    public Result findProcessType(){
        //return Result.ok(oaProcessTypeService.findProcessType());
        List<ProcessType> list = oaProcessTypeService.findProcessType();
        return Result.ok(list);
    }

    //获取审批模板数据
    @GetMapping(value="/getProcessTemplate/{processTemplateId}")
    public Result getProcessTemplate(@PathVariable Long processTemplateId){
        ProcessTemplate processTemplate = oaProcessTemplateService.getById(processTemplateId);
        return Result.ok(processTemplate);
    }

    @ApiOperation(value="启动流程")
    @PostMapping(value="/startUp")
    public Result startUp(@RequestBody ProcessFormVo processFormVo){
        oaProcessService.startUp(processFormVo);
        Process process = new Process();
        oaProcessRecordService.record(process.getId(), 1, "发起申请");
        return Result.ok();
    }


    @ApiOperation(value="已处理")
    @GetMapping(value="/findProcessed/{page}/{limit}")
    public Result findProcessed(@ApiParam(name="page",value="当前页码",required = true)
                                @PathVariable Long page,
                                @ApiParam(name ="limit",value="每页记录数", required= true)
                                @PathVariable Long limit){
        Page<Process> pageParam  = new Page<>(page,limit);
        return Result.ok(oaProcessService.findProcessed(pageParam));
    }

    @ApiOperation(value = "已发起")
    @GetMapping(value="/findStarted/{page}/{limit}")
    public Result findStarted(
            @ApiParam(name="page",value="当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit",value="每页记录数",required=true)
            @PathVariable Long limit
    ){
        Page<ProcessVo> pageParam = new Page<>(page,limit);
        return Result.ok(oaProcessService.findStarted(pageParam));
    }
}
